package com.example.trans.controller;


import com.example.trans.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@Slf4j
@RequestMapping("/trans")
@RestController
public class TransactionalController {
    @Autowired
    private UserService userService;

    //不添加事务的情况
//    @Transactional
    @RequestMapping("/r1")
    public String registry(String name, String password) {
        //用户注册
        Integer result = userService.registryUser(name, password);
        if (result == 1) {
            log.info("数据插入成功,影响行数:" + result);
        }
        int a = 10 / 0;
        return "注册成功";
    }

    //方法抛出异常,程序回滚;方法未发生异常,程序提交
    @Transactional
    @RequestMapping("/r2")
    public String r2(String name, String password) {
        //用户注册
        Integer result = userService.registryUser(name, password);
        if (result == 1) {
            log.info("数据插入成功,影响行数:" + result);
        }
        int a = 10 / 0;
        return "注册成功";
    }

    //异常被捕获:事务提交
    @Transactional
    @RequestMapping("/r3")
    public String r3(String name, String password) {
        //用户注册
        Integer result = userService.registryUser(name, password);
        if (result == 1) {
            log.info("数据插入成功,影响行数:" + result);
        }
        try {
            int a = 10 / 0;
        } catch (Exception e) {
            log.error("发生异常");
        }
        return "注册成功";
    }

    //被捕获,也想让他发生异常,事务回滚
    @Transactional
    @RequestMapping("/r4")
    public String r4(String name, String password) {
        //用户注册
        Integer result = userService.registryUser(name, password);
        if (result == 1) {
            log.info("数据插入成功,影响行数:" + result);
        }
        try {
            int a = 10 / 0;
        } catch (Exception e) {
            log.error("发生异常");
            throw e;
        }
        return "注册成功";
    }

    //手动回滚事务
    @Transactional
    @RequestMapping("/r5")
    public String r5(String name, String password) {
        //用户注册
        Integer result = userService.registryUser(name, password);
        if (result == 1) {
            log.info("数据插入成功,影响行数:" + result);
        }
        try {
            int a = 10 / 0;
        } catch (Exception e) {
            log.error("发生异常");
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return "注册成功";
    }

    //发生非运行时异常,事务提交
    @Transactional
    @RequestMapping("/r6")
    public String r6(String name, String password) throws IOException {
        //用户注册
        Integer result = userService.registryUser(name, password);
        if (result == 1) {
            log.info("数据插入成功,影响行数:" + result);
        }
        if (true) {
            throw new IOException();
        }
        return "注册成功";

    }

    //设置rollbackfor,发生非运行异常,事务回滚
    @Transactional(rollbackFor = Exception.class)
    @RequestMapping("/r7")
    public String r7(String name, String password) throws IOException {
        //用户注册
        Integer result = userService.registryUser(name, password);
        if (result == 1) {
            log.info("数据插入成功,影响行数:" + result);
        }
        if (true) {
            throw new IOException();
        }
        return "注册成功";
    }
    //设置事务的隔离级别
    @Transactional(isolation = Isolation.READ_COMMITTED)
    @RequestMapping("/r8")
    public String r8(String name, String password) throws IOException {
        //用户注册
        Integer result = userService.registryUser(name, password);
        if (result == 1) {
            log.info("数据插入成功,影响行数:" + result);
        }
        if (true) {
            throw new IOException();
        }
        return "注册成功";
    }
}
